﻿using log4net;
using StoreProject.BLL.StackBLL;
using StoreProject.DAL.StackDAL;
using StoreProject.Entity.StackEntity;
using StoreProject.Enum;
using StoreProject.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.ComponentModel.Design.ObjectSelectorEditor;

namespace StoreProject.Forms.StackForms.Operate
{
    public partial class StackAllotQueryForm : ConfigQueryExForm
    {
        ILog _log = LogManager.GetLogger("StackAllotQueryForm");
        ProductBLL productBLL = new ProductBLL();
        StackInBLL stackInBLL = new StackInBLL();
        StackLogBLL stackLogBLL = new StackLogBLL();
        public StackAllotQueryForm()
        {
            InitializeComponent();
        }

        private void StackAllotQueryForm_Load(object sender, EventArgs e)
        {
            DataGridViewButtonColumn btn2 = new DataGridViewButtonColumn();
            btn2.Name = "allot";
            btn2.HeaderText = " ";
            btn2.DefaultCellStyle.NullValue = "调拨";
            uiDataGridView1.Columns.Add(btn2);
            DataGridViewButtonColumn btn3 = new DataGridViewButtonColumn();
            btn3.Name = "stackout";
            btn3.HeaderText = " ";
            btn3.DefaultCellStyle.NullValue = "出库";
            uiDataGridView1.Columns.Add(btn3);

            var list = productBLL.GetList().OrderByDescending(x => x.UpdateTime).ToList().Select(x => x.ProductName).ToArray();
            this.cmbProductName.Items.AddRange(list);

            uiDataGridView1.AutoGenerateColumns = false;
            this.Query();
        }

        public override void Query()
        {
            try
            {
                List<StackInEntity> data = new List<StackInEntity>();
                if (!string.IsNullOrEmpty(this.cmbProductName.Text))
                {
                    data = stackInBLL.GetListByWhere(string.Format("ProductName like  '%{0}%' ", this.cmbProductName.Text)).ToList();
                }
                else
                {
                    data = stackInBLL.GetListEx().ToList();
                }
                if (data != null && data.Count > 0)
                {
                    this.uiDataGridView1.DataSource = data.OrderByDescending(x => x.UpdateTime).ToList(); ;
                }
                else
                {
                    uiDataGridView1.DataSource = null;
                    //cmbCustomer.DataSource = data;
                }
            }
            catch (Exception ex)
            {
                _log.Error("Query", ex);
            }
        }

        private void uiDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //点击button按钮事件
            if (uiDataGridView1.Columns[e.ColumnIndex].Name == "allot" && e.RowIndex >= 0)
            {
                int index = uiDataGridView1.SelectedCells[0].RowIndex;
                //获取所在行
                var Id = uiDataGridView1.Rows[index].Cells[0].Value;
                StackInEntity stackInEntity = stackInBLL.Get(Id.ToString());
                StackAllotSettingForm stackAllotSettingForm = new StackAllotSettingForm(stackInEntity);
                stackAllotSettingForm.TopMost = true;
                stackAllotSettingForm.ShowDialog();
                stackAllotSettingForm.BringToFront();
                this.Query();
            }
            else if (uiDataGridView1.Columns[e.ColumnIndex].Name == "stackout" && e.RowIndex >= 0)
            {
                int index = uiDataGridView1.SelectedCells[0].RowIndex;
                //获取所在行
                var Id = uiDataGridView1.Rows[index].Cells[0].Value;
                StackInEntity stackInEntity = stackInBLL.Get(Id.ToString());
                StackOutSettingForm stackOutSettingForm = new StackOutSettingForm(stackInEntity);
                stackOutSettingForm.TopMost = true;
                stackOutSettingForm.ShowDialog();
                stackOutSettingForm.BringToFront();
                this.Query();
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.Query();
        }
    }
}
